﻿Imports system.Data.SqlClient
Public Class SuaLop

    Dim Lop_ID As Object

    Private Property MaLop As String


    Private Sub button1_Click(sender As System.Object, e As System.EventArgs) Handles button1.Click
        Dim con As SqlConnection = New SqlConnection()
        con.ConnectionString = Ketnoi.str
        con.Open()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.Connection = con
        Dim TenKhoa As String
        TenKhoa = cboKhoa.SelectedItem.ToString()
        cmd.CommandText = "SELECT Khoa_ID FROM Khoa WHERE TenKhoa='" + TenKhoa + "'"
        Dim rd As SqlDataReader
        rd = cmd.ExecuteReader()
        Dim td As DataTable = New DataTable()
        td.Load(rd)
        Dim MaKhoa As String
        MaKhoa = td.Rows(0)(0).ToString()
        cmd.CommandText = "UPDATE Lop SET TenLop='" + txtTenLop.Text + "',ID_Khoa='" + MaKhoa + "' WHERE Lop_ID='" + MaLop + "'"
        Dim result As DialogResult
        result = MessageBox.Show("BẠN CÓ MUỐN SỬA LẠI DỮ LIỆU KHÔNG?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If result = DialogResult.Yes Then
            cmd.ExecuteNonQuery()
            con.Close()
            MessageBox.Show("SỬA DỮ LIỆU THÀNH CÔNG")
            Me.Close()
            Dim frm As DanhSachLop = New DanhSachLop(Lop_ID)
            frm.Show()
        End If
    End Sub

    Private Sub button2_Click(sender As System.Object, e As System.EventArgs) Handles button2.Click
        Me.Close()
        Dim frm As DanhSachLop = New DanhSachLop(Lop_ID)
        frm.Show()
    End Sub

    Private Sub SuaLop_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim con As SqlConnection = New SqlConnection()
        con.ConnectionString = Ketnoi.str
        con.Open()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.Connection = con
        cmd.CommandText = "SELECT * FROM Khoa"
        Dim rd As SqlDataReader
        rd = cmd.ExecuteReader()
        Dim td As DataTable = New DataTable()
        td.Load(rd)
        Dim i As Integer
        For i = 0 To td.Rows.Count - 1 Step i + 1
            Me.cboKhoa.Items.Add(td.Rows(i)(1))
        Next
        cmd.CommandText = "SELECT * FROM Lop WHERE Lop_ID='" + MaLop + "'"
        Dim rd1 As SqlDataReader
        rd1 = cmd.ExecuteReader()
        Dim td1 As DataTable = New DataTable()
        td1.Load(rd1)
        Me.txtMaLop.Text = td1.Rows(0)(0).ToString()
        Me.txtTenLop.Text = td1.Rows(0)(1).ToString()
        con.Close()
    End Sub
End Class